package config;


import com.MyHandlerAdapter;
import com.MyHandlerMapping;
import com.MyViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableWebMvc
@ComponentScan("com")
public class AppConfig implements WebMvcConfigurer {

    /*@Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/", ".jsp");
    }
*/

    @Bean
    public ViewResolver viewResolver() {
        MyViewResolver viewResolver = new MyViewResolver();
        viewResolver.setPrefix("/WEB-INF/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public HandlerMapping myHandlerMapping() throws ClassNotFoundException {
        MyHandlerMapping handlerMapping = new MyHandlerMapping();
        Map<String, Class<?>> map = new HashMap<>();
        map.put("/a", Class.forName("com.AHandler"));
        map.put("/b", Class.forName("com.BHandler"));


        handlerMapping.setUrlMapping(map);
        return handlerMapping;
    }


    @Bean
    public HandlerAdapter handlerAdapter() {
        return new MyHandlerAdapter();
    }
}
